package com.baidu.bos.web.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.baidu.bos.domain.Region;
import com.baidu.bos.service.IRegionService;
import com.baidu.bos.utils.PinYin4jUtils;
import com.baidu.bos.web.action.base.BaseAction;

@Controller
@Scope("prototype")
public class RegionAction extends BaseAction<Region> {
	@Resource
	private IRegionService regionService;
	
	private File myFile;
	private String myFileFileName;
	public void setMyFileFileName(String myFileFileName) {
		this.myFileFileName = myFileFileName;
	}
	public void setMyFile(File myFile) {
		this.myFile = myFile;
	}
	/**
	 * 导入
	 * @return
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public String upload() throws FileNotFoundException, IOException {
		String flag = "1";// 导入是否成功的标识
		// 创建一个List对象，包装区域对象
		List<Region> list = new ArrayList<Region>();
		try {
			Workbook workbook = null;
			if (myFileFileName.endsWith(".xls")) {
				//1.加载xls文件
				workbook = new HSSFWorkbook(
						new FileInputStream(myFile));
			}else if(myFileFileName.endsWith(".xlsx")){
				//加载xlsx文件
				workbook = new XSSFWorkbook(
						new FileInputStream(myFile));
			}
			//获取第一个sheet
			Sheet sheet = workbook.getSheetAt(0);
			for (Row row : sheet) {
				int rowNum = row.getRowNum();
				if (rowNum == 0) {
					// 第一行为文件的标题行，忽略
					continue;
				}
				String id = row.getCell(0).getStringCellValue();
				String province = row.getCell(1).getStringCellValue();
				String city = row.getCell(2).getStringCellValue();
				String district = row.getCell(3).getStringCellValue();
				String postcode = row.getCell(4).getStringCellValue();
				
				
				Region region = new Region(id, province, city, district, postcode);
				// 使用Pinyin4J工具生成简码和城市编码

				// 简码----北京市北京市东城区----》BJSBJSDCQ
				String shortcode = province + city + district;
				String[] strings = PinYin4jUtils.getHeadByString(shortcode);
				shortcode = StringUtils.join(strings, "");

				// 城市编码----北京市-----》beijingshi
				String[] strings2 = PinYin4jUtils.stringToPinyin(city);
				String citycode = StringUtils.join(strings2);
				
				region.setCitycode(citycode);
				region.setShortcode(shortcode);
				
				list.add(region);
			}
			//批量保存数据
			regionService.saveBatch(list);
		} catch (Exception e) {
			e.printStackTrace();
			flag = "0";
		}
		ServletActionContext.getResponse().getWriter().print(flag);
		return NONE;
	}
	
	/**
	 * 分页条件查询
	 */
	public String pageQuery(){
		regionService.pageQuery(pageBean);
		this.writeObject2Json(pageBean);
		return NONE;
	}
	
	public String findRegionByAjax(){
		//查询出所有的区域加载到页面
		List<Region> list = regionService.findAll();
		this.writeObject2Json(list);
		return NONE;
	}
}
